<template>
  <div class="app-container">
    <!-- 搜索 -->
    <div class="filter-container">
      <el-input v-model="listQuery.search" class="filter-item" style="width: 200px" placeholder="用例名称" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter"> 查询</el-button>
      <el-button class="filter-item" type="primary" @click="handleConnect">关联用例</el-button>
    </div>
    <!-- 数据 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column v-show="false" label="ID" prop="id" aligin="center" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用例名称" prop="case_name">
        <template slot-scope="scope">
          <span>{{ scope.row.case_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用例类型" prop="case_type">
        <template slot-scope="scope">
          <span>{{ typeOptions[scope.row.case_type] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="add_time">
        <template slot-scope="scope">
          <span>{{ dateFormat(scope.row.add_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" prop="edit_time">
        <template slot-scope="scope">
          <span>{{ dateFormat(scope.row.edit_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="{row}">
          <el-button type="primary" size="small" @click="handleCheck(row)">查看</el-button>
          <el-button type="danger" size="small" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <!-- 查看详情dialog -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :model="temp" label-position="left" label-width="70px" style="width: 800px; margin-left:50px;">
        <el-form-item label="计划名称" prop="plan_num">
          <span>{{ temp.plan_name }}</span>
        </el-form-item>
        <el-form-item label="用例编号" prop="case_num">
          <el-input v-model="temp.case_num" readonly />
        </el-form-item>
        <el-form-item label="用例名称" prop="case_name">
          <el-input v-model="temp.case_name" readonly />
        </el-form-item>
        <el-form-item label="操作步骤">
          <el-table v-loading="stepLoading" :data="stepList" border highlight-current-row>
            <el-table-column label="顺序" prop="step_num">
              <template slot-scope="scope">
                <span>{{ scope.row.step_num }}</span>
              </template>
            </el-table-column>
            <el-table-column label="步骤名称" prop="name">
              <template slot-scope="scope">
                <span>{{ scope.row.name }}</span>
              </template>
            </el-table-column>
            <el-table-column label="类型" prop="type">
              <template slot-scope="scope">
                <span>{{ typeOptions[scope.row.type] }}</span>
              </template>
            </el-table-column>
            <el-table-column label="参数" prop="params" width="400px">
              <template slot-scope="scope">
                <el-table :data="JSON.parse(scope.row.params)">
                  <el-table-column label="参数名" prop="name">
                    <template slot-scope="scope">
                      <span>{{ scope.row.name }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="参数类型" prop="type">
                    <template slot-scope="scope">
                      <span>{{ scope.row.type }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="参数值" prop="value">
                    <template slot-scope="scope">
                      <span>{{ scope.row.value }}</span>
                    </template>
                  </el-table-column>
                </el-table>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          关闭
        </el-button>
      </div>
    </el-dialog>
    <!--关联测试用例dialog-->
    <el-dialog :visible.sync="dialogTableVisible" title="关联测试用例">
      <div style="margin-bottom: 10px">
        <el-input v-model="caseListQuery.search" style="width: 160px" />
        <el-button style="margin-left: 20px" @click="handlerFilterCase">查询</el-button>
      </div>
      <el-table v-loading="caseLoading" :data="caseList" border highlight-current-row @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="45px" />
        <el-table-column label="ID" prop="id" width="70px">
          <template slot-scope="scope">
            <span>{{ scope.row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column label="用例编号" prop="case_num">
          <template slot-scope="scope">
            <span>{{ scope.row.case_num }}</span>
          </template>
        </el-table-column>
        <el-table-column label="用例标题" prop="name">
          <template slot-scope="scope">
            <span>{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="add_time">
          <template slot-scope="scope">
            <span>{{ dateFormat(scope.row.add_time) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="修改时间" prop="edit_time">
          <template slot-scope="scope">
            <span>{{ dateFormat(scope.row.edit_time) }}</span>
          </template>
        </el-table-column>
      </el-table>
      <Pagination v-show="caseTotal>0" :total="caseTotal" :page.sync="caseListQuery.page" :limit.sync="caseListQuery.limit" />
      <el-button type="primary" @click="relateCase">确定</el-button>
      <el-button type="warning" @click="dialogTableVisible=false">取消</el-button>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { getPlanCaseList, getPlanCase, deletePlanCase, createPlanCases } from '@/api/planCase'
import { getCaseList } from '@/api/case'
import { mapGetters } from 'vuex'
import { dateFormat } from '@/utils/data'
export default {
  name: 'Project',
  components: { Pagination },
  data() {
    return {
      list: [],
      total: 0,
      stepList: [],
      caseList: [],
      caseTotal: 0,
      caseLoading: false,
      stepLoading: false,
      stepTotal: 0,
      stepPage: 1,
      stepLimit: 20,
      planId: 0,
      listQuery: {
        search: '',
        plan: 0,
        page: 1,
        limit: 20
      },
      typeOptions: {
        web: 'Selenium',
        app: '移动端',
        interface: 'HTTP接口',
        sql: '数据库'
      },
      listLoading: false,
      dialogFormVisible: false,
      temp: {
        id: undefined,
        plan_name: '',
        case_name: 'web',
        case_detail: ''
      },
      dialogTitle: '详情',
      dialogTableVisible: false,
      caseListQuery: {
        search: '',
        page: 1,
        limit: 20
      },
      multipleSelection: []
    }
  },
  computed: {
    ...mapGetters([
      'name'
    ])
  },
  created() {
    this.planId = this.$route.params && this.$route.params.id
    console.log('listQuery:', this.listQuery)
    this.listQuery.plan = this.planId
    this.getList()
  },
  methods: {
    getList() {
      getPlanCaseList(this.listQuery).then((res) => {
        this.listLoading = true
        this.list = res.results
        this.total = res.count
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    dateFormat: function(attr) {
      return dateFormat(attr)
    },
    getCaseList() {
      getCaseList(this.caseListQuery).then((response) => {
        this.caseList = response.results
        this.caseTotal = response.count
      })
    },
    handlerFilterCase() {
      this.getCaseList()
    },
    handleFilter() {
      console.log('goto search')
      this.listQuery.page = 1
      this.getList()
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        plan_name: '',
        case_name: 'web',
        case_detail: ''
      }
    },
    handleCheck(row) {
      console.log('go to edit!')
      this.stepList = []
      this.temp = Object.assign({}, row)
      this.dialogFormVisible = true
      getPlanCase(row.id).then((response) => {
        this.temp = Object.assign({}, response.data)
        console.log('case_detail: ', response.data.case_detail)
        const steps = JSON.parse(response.data.case_detail)
        for (const s in steps) {
          console.log('s:', s)
          this.stepList.push(steps[s].fields)
        }
        console.log('stepList:', this.stepList)
      })
    },
    getPlanCaseDetail(id) {
      getPlanCase(id).then((response) => {
        this.temp = Object.assign({}, response.data)
      })
    },
    handleDelete(row) {
      this.$confirm('确认要删除该数据', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then((action) => {
        if (action === 'confirm') {
          deletePlanCase(row.id).then((res) => {
            if (res) {
              const index = this.list.indexOf(row)
              this.list.splice(index, 1)
              this.$message({
                type: 'success',
                message: '删除成功'
              })
            }
          })
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消操作'
        })
      })
    },
    handleConnect() {
      this.dialogTableVisible = true
      this.getCaseList()
    },
    handleSelectionChange(row) {
      this.multipleSelection = row
      console.log(this.multipleSelection)
    },
    relateCase() {
      if (this.multipleSelection.length) {
        const caseIds = []
        for (const i in this.multipleSelection) {
          caseIds.push(this.multipleSelection[i].id)
        }
        createPlanCases(this.planId, caseIds).then(response => {
          this.$notify({
            title: '提示',
            type: 'success',
            message: '关联成功',
            duration: 2000
          })
          this.dialogTableVisible = false
          this.getList()
        })
      } else {
        this.$notify({
          title: '提示',
          type: 'error',
          message: '至少选中一个用例',
          duration: 2000
        })
      }
    }
  }
}
</script>

<style scoped>

</style>
